/* 是否手机号码或者固话*/
export function validatePhoneTwo(value) {
  const reg = /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/
  return reg.test(value) || value === '' || value === undefined || value == null
}

/* 是否固话*/
export function validateTelephone(value) {
  const reg = /0\d{2}-\d{7,8}/
  return reg.test(value) || value === '' || value === undefined || value == null
}

/* 是否手机号码*/
export function validatePhone(value) {
  // const reg = /^[1][0-9]{10}$/;
  const reg = /^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\d{8}$/
  return reg.test(value)
}
/* 是否手机号码，为空不校验-健康档案用 */
export function validatePhoneCanNull(value) {
  // const reg = /^[1][0-9]{10}$/;
  const reg = /^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\d{8}$/
  return !value || reg.test(value)
}
/* 是否身份证号码*/
export function validateIdNo(value) {
  // const reg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  // let validYmd = false
  // if (value && reg.test(value)) {
  //   const yearLimit = Number(value.slice(6, 7))
  //   const year = Number(value.slice(6, 10))
  //   const month = Number(value.slice(10, 12))
  //   const day = Number(value.slice(12, 14))
  //   const _date = new Date(year, month - 1, day)
  //   validYmd = (yearLimit === 1 || yearLimit === 2) && year === _date.getFullYear() && month === _date.getMonth() + 1 && day === _date.getDate()
  // }
  // return (reg.test(value) && validYmd) || value === '' || value === undefined || value == null
  const reg = /^\d{6}((19|20)?\d{2})(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}([0-9Xx])?$/

  return reg.test(value)
}
/** 校验身份证号，为空不校验-健康档案用 */
export function validateIdNoCanNull(value) {
  const reg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  let validYmd = false
  if (value && reg.test(value)) {
    const yearLimit = Number(value.slice(6, 7))
    const year = Number(value.slice(6, 10))
    const month = Number(value.slice(10, 12))
    const day = Number(value.slice(12, 14))
    const _date = new Date(year, month - 1, day)
    validYmd = (yearLimit === 1 || yearLimit === 2) && year === _date.getFullYear() && month === _date.getMonth() + 1 && day === _date.getDate()
  }
  return !value || (reg.test(value) && validYmd)
}

export function valideFieldsInRow(row, requiredKeys) {
  let flag = true
  for (let i = 0; i < requiredKeys.length; i++) {
    if (row[requiredKeys[i].key] !== 0 && !row[requiredKeys[i].key]) {
      uni.showToast({
        title: requiredKeys[i].tip,
        duration: 2000,
        position: 'bottom' // 仅App生效
      })
      flag = false
      break
    }
  }
  return flag
}

/**
 *
 * @param form 表单对象 {a: 1, b: 2}
 * @param expect 期望校验的必填项 ['a', 'b']
 * @returns {boolean}
 */
export function isFormReady(form, expect) {
  let flag = false
  for (let item of expect) {
    // 解决为0判断失误问题
    if (form[item] === undefined || form[item] === null || form[item] === '') {
      flag = true
      return flag
    }
  }
  // document.querySelectorAll('.van-field__error-message')
  // 若表单中有表单校验未通过的 也返回true
  // const nodeList = document.querySelectorAll('.van-form .van-field__error-message')
  // if(nodeList && nodeList.length){
  //   flag = true
  //   return flag
  // }
  return flag
}

/**
 * 校验温度是否合理
 */
export function validateTemperature(value) {
  console.log('validateTemperature>>>' + value)
  const reg = /((^3[0-9])(\.\d{1})?$|^(4[0-6])(\.\d{1})?$)/
  return reg.test(value)
}
